#ifndef SRC_DEVICE_SIMULATOR_RTSP_SERVER_H_
#define SRC_DEVICE_SIMULATOR_RTSP_SERVER_H_

#include <list>
#include <map>
#include "base/event_loop.h"
#include "base/listener.h"
#include "base/async_socket.h"


class RtspServer : public noncopyable,
  public std::enable_shared_from_this<RtspServer>,
  public sigslot::has_slots<> {
 public:
  typedef std::shared_ptr<RtspServer> Ptr;
  struct ClientInfo {
    RtspServer::Ptr rtsp_server;
    AsyncSocket::Ptr async_socket;
    struct timeval base_tv;
    struct event *video_evtimer;
    struct timeval video_tvtimer;
  };
  explicit RtspServer(EventLoop::Ptr event_loop);
  virtual ~RtspServer();

 public:
  bool Start(const std::string &ip, int port);

 public:
  void OnVideoTimer(AsyncSocket::Ptr async_socket);

 private:
  void OnListenerAccept(Listener::Ptr listener,
                        evutil_socket_t fd,
                        struct sockaddr *saddr,
                        int slen);
  void OnListenerError(Listener::Ptr listener);
  void OnAsyncSocketPacket(AsyncSocket::Ptr async_socket,
                           uint16_t cmd,
                           const char *data,
                           uint32_t data_size);
  void OnAsyncSocketError(AsyncSocket::Ptr async_socket, int error_code);
  void OnAsyncSocketClosed(AsyncSocket::Ptr async_socket);

 private:
  EventLoop::Ptr event_loop_;
  Listener::Ptr listener_;
  std::map<AsyncSocket::Ptr, ClientInfo*> client_map_;
  char video_frame_data_[11264 - sizeof(PacketHeader)];
};

#endif  // SRC_DEVICE_SIMULATOR_RTSP_SERVER_H_
